package com.richy.toweather.model;

import com.richy.toweather.contract.MainContract;
import com.richy.toweather.entity.WeatherEntity;
import com.richy.toweather.net.WeatherService;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @FileName MainModel
 * @Description MainModel
 * @Author x925914554@gmail.com
 * @Company
 * @CreateDate 18-11-13
 * @LastModifyDate 18-11-13
 * @Since
 * @Version
 */
public class MainModel implements MainContract.Model {
    private static final String uid = "800ee88d7d154037afe0662d7e082e34";
    private static final String BASE_URI = "https://free-api.heweather.com";
    private WeatherService mWeatherService;
    private CallBack mCallBack;

    public MainModel(CallBack callBack) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URI)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        mWeatherService = retrofit.create(WeatherService.class);
        mCallBack = callBack;
    }

    @Override
    public void getData(String city) {
        final List<WeatherEntity> weatherEntities = new ArrayList<>();
        mWeatherService.getWeather(uid, city)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WeatherEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WeatherEntity weatherEntity) {
                        mCallBack.onData(weatherEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mCallBack != null) {
                            mCallBack.onDataError(e);
                        }
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }


    @Override
    public void getDatas(String[] cities) {

        final List<WeatherEntity> weatherEntities = new ArrayList<>();
        final int length = cities.length;

        for (String city : cities) {
            mWeatherService.getWeather(uid, city)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<WeatherEntity>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(WeatherEntity weatherEntity) {
                            weatherEntities.add(weatherEntity);
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (mCallBack != null) {
                                mCallBack.onDataError(e);
                            }
                        }

                        @Override
                        public void onComplete() {
                            if (length == weatherEntities.size()) {
                                if (mCallBack != null) {
                                    mCallBack.onDatas(weatherEntities);
                                }
                            }
                        }
                    });

        }


    }

    public interface CallBack {
        void onDataError(Throwable e);

        void onData(WeatherEntity weatherEntity);

        <T> void onDatas(List<T> weatherEntities);
    }
}
